let checkCodeInterval;
$(function(){
    $('#phone').blur(function(){
        if (isNull(V('phone'))){
            setErrMsg('请输入手机号');
            return;
        }
        verifyUserPhone(this);
    });
    $('#btnGetCode').click(function () {
		//60秒倒计时
    	let time = 60, btnGetCode = this;
		btnGetCode.disabled = true;
  		checkCodeInterval = window.setInterval(function () {
  			time --;
  			if (time > 0) {
				$(btnGetCode).text(time + '秒后可发送');
			}else {
				setButtonStatus(btnGetCode,false,'获取验证码');
			}
		},1000);
  		//获取验证码
  		goSendCheckCode(btnGetCode);
    });
    $('#btnUpdatePwd').click(function() {
    	this.disabled = true;
    	sureModifyPwd(this);
	});
});

/**
 * 验证用户手机号是否存在
 */
function verifyUserPhone(btnObj) {
	//从后台判断用户名是否存在
	let fpLoginName = V('phone');
	if (!phoneNumberIsRight(fpLoginName)){
		setErrMsg('手机号码错误');
		btnObj.disabled = false;
		return;
	}
    $.ajax({
		type: "POST",
		url: '/ventUserIndex/isExistsPhone',
		data: {phone : fpLoginName},
		dataType: 'json',
		async: false,
		success: function(verifyResult) {
			btnObj.disabled = false;
			let s = '用户名不存在或已被冻结';
			if (verifyResult){
				s = '';
			}
			setErrMsg(s);
		},
		error: function() {
			btnObj.disabled = false;
			setErrMsg( '系统繁忙，请稍候再试');
		}
	});
}
//获取验证码
function goSendCheckCode(sendBtn) {
	let phone = V('phone');
	if (!phoneNumberIsRight(phone)){
		setErrMsg('请输入正确的手机号');
		window.clearInterval(checkCodeInterval);
		setButtonStatus(sendBtn,false,'获取验证码');
		return;
	}
    $.ajax({
        type: "POST",
        url: '/ventUserIndex/sendCodeByNoLogin',
        data:{phone: phone},
        dataType: 'json',
        async: false,
        success: function(verifyResult) {
        	let vs = verifyResult.status,errMsg = '当前系统繁忙,请稍后再获取验证码';
        	if(vs == '1'){
				errMsg = '验证码已发送';
        	}else if(vs == '3'){
                errMsg = '该手机号码未注册，请重新填写手机号';
        	}else if(vs == '4'){
                errMsg = '您当前IP地址获取验证码次数过多，请稍候再试';
        	}else if(vs == '5'){
                errMsg = '您当前手机号码获取验证码次数过多，请稍候再试';
        	}else if(vs == '6'){
				errMsg = '手机号码格式不对';
			}
			setErrMsg(errMsg);
        },
        error: function() {
			setErrMsg('当前系统繁忙,请稍后再获取验证码');
			setButtonStatus(sendBtn,false,'获取验证码');
			window.clearInterval(checkCodeInterval);
		}
    });
}
function setErrMsg(s) {
	$('#errMsg').html(s);
}
/**
 * 确认修改密码
 */
function sureModifyPwd(btnSureObj) {
	let mpCheckCode = V('checkCode'),newPwd = V('newPwd'),myPhone = V('phone');
	if (isNull(myPhone)) {
		setErrMsg('手机号不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(mpCheckCode)) {
		setErrMsg('验证码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if (isNull(newPwd)) {
		setErrMsg('新密码不能为空');
		btnSureObj.disabled = false;
		return;
	}
	if(!confirm('您确认要修改密码吗？')){
		btnSureObj.disabled = false;
		return;
	}
	let shaObj = new jsSHA("SHA-1", "TEXT");
	shaObj.update(newPwd);
	newPwd = shaObj.getHash("HEX");
	$.ajax({
		type: "POST",
		url: '/ventUserIndex/modifyPwdByCodeWithNoLogin',
		data:{myPhone: myPhone,checkCode: mpCheckCode,newPwd: newPwd},
		dataType: 'json',
		success: function(modifyResult) {
			let ms = modifyResult.status;
			if(ms == '1'){
				alert('修改密码时出错');
			}else if(ms == '2'){
				alert('未获取到验证码');
			}else if(ms == '3'){
				alert('未获取到手机号');
			}else if(ms == '4'){
				alert('未获取到用户密码');
			}else if(ms == '5'){
				alert('验证码错误或已失效');
			}else if(ms == '0'){
				alert('密码修改成功,快去登录吧');
				window.location.href = 'ventLogin.html';
			}else {
				alert('密码修改失败');
			}
			btnSureObj.disabled = false;
		},
		error: function() {
			alert('当前系统繁忙,请稍后再获取验证码');
			btnSureObj.disabled = false;
		}
	});
}